<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
          @orgsearch="orgsearch"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '../common/basicModal'
import basicForm from '../common/basicForm'
import { getOrgInfoByPid } from '@/views/sys/org/org/org'
import { getInfoByPid, saveInfo,afterSubmit } from './supplier'

export default {
  components: {
    basicModal,
    basicForm,
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增品种',
        modalWidth: '1000px',
      },
      newOrg: true,
      s_FormInfo: [
        { label: '请输入供应商单位社会信用代码', isDisabled: false, name: 'gongYingsshxxdm', type: 'orgsearch',  placeholder: '' },
        { label: '供应商编码', name: 'gongYingsdm', isDisabled: true, type: 'input', placeholder: ' 供应商编码' },
        // { label: '',name:'keHushxxdm',type:'input',placeholder:' 客户社会信用代码'},
        { label: '供应商名称', name: 'gongYingsmc', isDisabled: true, type: 'input', placeholder: ' 供应商名称' },
        { label: '城市', name: 'chengShi', isDisabled: true, type: 'input', placeholder: ' 城市' },
        { label: '地点', name: 'diZhi', isDisabled: true, type: 'input', placeholder: ' 地点' },
        { label: '负责人姓名', name: 'fuZerxm', isDisabled: true, type: 'input', placeholder: ' 负责人姓名' },
        { label: '负责人联系电话', name: 'fuZerdh', isDisabled: true, type: 'input', placeholder: ' 负责人联系电话' },
        { label: '联系人姓名', name: 'lianXirxm', isDisabled: true, type: 'input', placeholder: ' 联系人姓名' },
        { label: '联系人联系电话', name: 'lianXirdh', isDisabled: true, type: 'input', placeholder: ' 联系人联系电话' },
      ],
      s_formDatas: {
        sysSeq: 0,
        pid: '',
        gongYingsdm: null,
        gongYingsOrgPid: null,
        gongYingsshxxdm: null,
        gongYingsmc: null,
        chengShi: null,
        diZhi: null,
        fuZerxm: null,
        fuZerdh: null,
        lianXirxm: null,
        lianXirdh: null,
        zhuangTai: null
      },
      s_rules: {
        gongYingsshxxdm: [{ required: true, message: '信用代码必填', trigger: 'blur' }],
        gongYingsmc: [{ required: true, message: '名称必填', trigger: 'blur' }],
        fuZerxm: [{ required: true, message: '负责人必填', trigger: 'change' }],
        lianXirxm: [{ required: true, message: '联系人必填', trigger: 'change' }],
        lianXirdh: [{ required: true, message: '联系人电话必填', trigger: 'blur' }],
      },
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 新增按钮 点击了新增
    addModal() {
      this.s_formDatas = {
        sysSeq: 0,
        pid: '',
        gongYingsdm: null,
        gongYingsOrgPid: null,
        gongYingsshxxdm: null,
        gongYingsmc: null,
        chengShi: null,
        diZhi: null,
        fuZerxm: null,
        fuZerdh: null,
        lianXirxm: null,
        lianXirdh: null,
        zhuangTai: null
      }
      for (let item of this.s_FormInfo) {
        if (item.name === 'gongYingsshxxdm') {
          item.isDisabled = false
        } else {
          item.isDisabled = true
        }
      }
      console.log('点击了新增')
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增供应商'
    },
    // 新增按钮 点击了新增
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      for (let item of this.s_FormInfo) {
        if (item.name === 'gongYingsshxxdm') {
          item.isDisabled = true
        } else {
          item.isDisabled = false
        }
      }
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改供应商信息'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.afterSubmit(res.data.gongYingsOrgPid)
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    //新增供应商成功后提交
    async afterSubmit(val){
      let res = await afterSubmit(val)
      console.log(res);
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      switch (name) {
        case 'gongYingsOrgPid':
          this.getOrgInfo(val)
          break
      }
    },
    // 初始化
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    },
    // 查询单位信息
    async getOrgInfo(val) {
      let res = await getOrgInfoByPid(val, {})
      if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas.keHumc = res.data.orgFullName
      }
      console.log(res)
    },
    // 单位搜索
    orgsearch(name, val) {
      console.log('val', val)
      console.log('name', name)
      this.s_formDatas.gongYingsdm = val.orgIdno
      this.s_formDatas.gongYingsshxxdm = val.orgIdno
      this.s_formDatas.gongYingsOrgPid = val.org.pid
      this.s_formDatas.gongYingsmc = val.org.orgName
      this.s_formDatas.chengShi = val.org.city
      this.s_formDatas.fuZerxm = val.org.orgBossName
      for (let item of this.s_FormInfo) {
        if (item.name === 'gongYingsshxxdm') {
          item.isDisabled = true
        } else {
          item.isDisabled = false
        }
      }
    },
  },
}
</script>

<style>
</style>